# -*- coding: utf-8 -*-
# @Time: 2021/8/23 9:42
# @File: arg-122.py
# @DESC:

def maxProfit(prices):
    # 低进，高出
    flag = 1  # 标记出售状态 0 表示持有 1 表示可买入
    buy_price = 0
    total_profit = 0
    for i in range(len(prices) - 1):
        cur = prices[i]
        next = prices[i + 1]
        # 当前状态是持有，并且下一个会跌，
        if next < cur and flag == 0:
            flag = 1
            total_profit += (cur - buy_price)
        # 涨到最后了，必须卖了
        if flag == 0 and next > cur and i == len(prices) - 2:
            total_profit += (next - buy_price)
        # 当前状态是没买进，并且下一个会涨
        if flag == 1 and next > cur:
            # 买入
            flag = 0
            buy_price = cur
    return total_profit


# 处理一下只有两次的情况
def maxProfit2(prices):
    # 低进，高出
    flag = 1  # 标记出售状态 0 表示持有 1 表示可买入
    buy_price = 0
    total_profit = 0
    for i in range(len(prices) - 1):
        cur = prices[i]
        next = prices[i + 1]
        # 当前状态是持有，并且下一个会跌，
        if next <= cur and flag == 0:
            flag = 1
            total_profit += (cur - buy_price)
        # 当前状态是没买进，并且下一个会涨
        if flag == 1 and next > cur:
            # 买入
            flag = 0
            buy_price = cur
        # 涨到最后了，必须卖了
        if flag == 0 and next > cur and i == len(prices) - 2:
            total_profit += (next - buy_price)
    return total_profit


prices = [1,9,6,9,1,7,1,1,5,9,9,9]

r = maxProfit2(prices)
print(r)
